package com.atguigu.daijia.dispatch.xxl.job;

import com.atguigu.daijia.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.daijia.dispatch.service.NewOrderService;
import com.atguigu.daijia.model.entity.dispatch.XxlJobLog;
import com.atguigu.daijia.model.vo.dispatch.NewOrderTaskVo;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 谢忠涵7
 */
@Component
public class JobHandler {

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;

    @Autowired
    private NewOrderService newOrderService;

    @XxlJob("newOrderTaskHandler")
    public void newOrderTaskHandler() {
        //主要就算记录日志
        XxlJobLog log = new XxlJobLog();
        log.setJobId(XxlJobHelper.getJobId());
        long currentTimeMillis = System.currentTimeMillis();
        try {
            //执行 方法
            NewOrderTaskVo newOrderTaskVo = new NewOrderTaskVo();
            newOrderService.execute(XxlJobHelper.getJobId());
            log.setStatus(1);
        }catch (Exception e) {
            log.setStatus(0);
            log.setError(e.getMessage());
            e.printStackTrace();
        }finally {
            long endTime = System.currentTimeMillis();
            log.setTimes(endTime-currentTimeMillis);
            xxlJobLogMapper.insert(log);
        }
    }
}
